//
//  XFYShareSDKAdapter.m
//  SDKSample
//
//  Created by 小肥羊 on 15/7/22.
//  Copyright (c) 2015年 Zhu‘s Game House. All rights reserved.
//

#import "XFYShareSDKAdapter.h"

@implementation XFYShareSDKAdapter

#pragma mark - 单例
+ (XFYShareSDKAdapter *)sharedManager
{
    static XFYShareSDKAdapter *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}

#pragma mark - 注册分享列表
/*! @brief 注册所有控件 */
- (void)registerAllShare {
    /**
     *  设置ShareSDK的appKey，如果尚未在ShareSDK官网注册过App，请移步到http://mob.com/login 登录后台进行应用注册，
     *  在将生成的AppKey传入到此方法中。
     *  方法中的第二个参数用于指定要使用哪些社交平台，以数组形式传入。第三个参数为需要连接社交平台SDK时触发，
     *  在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发，根据返回的平台类型来配置平台信息。
     *  如果您使用的时服务端托管平台信息时，第二、四项参数可以传入nil，第三项参数则根据服务端托管平台来决定要连接的社交SDK。
     */
    [ShareSDK registerApp:@"iosv1101"
          activePlatforms:@[@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeTencentWeibo), @(SSDKPlatformTypeFacebook), @(SSDKPlatformTypeTwitter), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ)]
                 onImport:^(SSDKPlatformType platformType) {
                     
                     switch (platformType)
                     {
                         case SSDKPlatformTypeWechat:
                             [ShareSDKConnector connectWeChat:[WXApi class]];
                             break;
                         case SSDKPlatformTypeQQ:
                             [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                             break;
                         default:
                             break;
                     }
                     
                 }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
              
              switch (platformType)
              {
                  case SSDKPlatformTypeSinaWeibo:
                      //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                      [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                                appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                              redirectUri:@"http://www.sharesdk.cn"
                                                 authType:SSDKAuthTypeBoth];
                      break;
                  case SSDKPlatformTypeTencentWeibo:
                      //设置腾讯微博应用信息，其中authType设置为只用Web形式授权
                      [appInfo SSDKSetupTencentWeiboByAppKey:@"801307650"
                                                   appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                                 redirectUri:@"http://www.sharesdk.cn"];
                      break;
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:@"wxa0ce0aefeca58b6e"
                                            appSecret:@"bc28c5e4b76d0cdb75ce08ebfafdba24"];
                      break;
                  case SSDKPlatformTypeQQ:
                      [appInfo SSDKSetupQQByAppId:@"100371282"
                                           appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                         authType:SSDKAuthTypeSSO];
                      break;
                  default:
                      break;
              }
              
          }];
    
    //设置用户类型为ShareSDKUserInfo，这样可以将自定义的用户信息类与第三方登录进行绑定
    [SSEThirdPartyLoginHelper setUserClass:[ShareSDKUserInfo class]];
}

#pragma mark - 第三方登录相关
- (void)LoginOut
{
    SSEBaseUser *currentUser = [SSEThirdPartyLoginHelper currentUser];
    //注销用户
    [SSEThirdPartyLoginHelper logout:currentUser];
}

- (ShareSDKUserInfo *)currentUser
{
    return (ShareSDKUserInfo *)[SSEThirdPartyLoginHelper currentUser];
}

#pragma mark - 分享相关
/*! @brief 指定平台快速分享 不显示分享菜单 */
- (void)ShareNoUIWithKey:(NSDictionary *)keyValue ShareType:(SSDKPlatformType)shareType onStateChanged:(SSDKShareStateChangedHandler)stateChangedHandler
{
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if (shareType == SSDKPlatformSubTypeWechatSession)
    {
        [shareParams SSDKSetupWeChatParamsByText:keyValue[__eShareBody]
                                           title:keyValue[__eShareTitle]
                                             url:[NSURL URLWithString:keyValue[__eShareUrl]]
                                      thumbImage:keyValue[__eShareImage]?keyValue[__eShareImage]
                                                :@""
                                           image:nil
                                    musicFileURL:nil
                                         extInfo:nil
                                        fileData:nil
                                    emoticonData:nil
                                            type:SSDKContentTypeWebPage
                              forPlatformSubType:SSDKPlatformSubTypeWechatSession];
    } else if (shareType == SSDKPlatformSubTypeWechatTimeline)
    {
        [shareParams SSDKSetupShareParamsByText:keyValue[__eShareBody]
                                         images:@[keyValue[__eShareImage]]
                                            url:[NSURL URLWithString:keyValue[__eShareUrl]]
                                          title:keyValue[__eShareTitle]
                                           type:SSDKContentTypeWebPage];
    } else
    {
        [shareParams SSDKSetupShareParamsByText:keyValue[__eShareBody]
                                         images:@[keyValue[__eShareImage]]
                                            url:[NSURL URLWithString:keyValue[__eShareUrl]]
                                          title:keyValue[__eShareTitle]
                                           type:SSDKContentTypeImage];
    }

    //进行分享
    [ShareSDK showShareEditor:shareType
           otherPlatformTypes:nil
                  shareParams:shareParams
          onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
              
              switch (state) {
                      
                  case SSDKResponseStateBegin:
                  {
                      break;
                  }
                  case SSDKResponseStateSuccess:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                          message:nil
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  case SSDKResponseStateFail:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                          message:[NSString stringWithFormat:@"%@", error]
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  case SSDKResponseStateCancel:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                          message:nil
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  default:
                      break;
              }
              
              if (state != SSDKResponseStateBegin)
              {
                  
              }
              
          }];
}


/*! @brief ShareSDK自带UI,选择分享 */
- (void)ShareToShareSdkUIWithKey:(NSDictionary *)keyValue View:(UIView *)view
{
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:keyValue[__eShareBody]
                                     images:@[keyValue[__eShareImage]]
                                        url:[NSURL URLWithString:keyValue[__eShareUrl]]
                                      title:keyValue[__eShareTitle]
                                       type:SSDKContentTypeImage];
    
    //1.1、QQ空间不支持SSDKContentTypeImage这种类型，所以需要定制下。
    [shareParams SSDKSetupQQParamsByText:keyValue[__eShareBody]
                                   title:keyValue[__eShareTitle]
                                     url:[NSURL URLWithString:keyValue[__eShareUrl]]
                              thumbImage:nil
                                   image:keyValue[__eShareImage]
                                    type:SSDKContentTypeWebPage
                      forPlatformSubType:SSDKPlatformSubTypeQZone];
    
    //1.2、设置分享编辑页面样式（optional）
    //    [SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:249/255.0 green:0/255.0 blue:12/255.0 alpha:0.5]];
    //    [SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
    //    [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
    //    [SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor whiteColor]];
    //    [SSUIShareActionSheetStyle setItemNameColor:[UIColor whiteColor]];
    //    [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]];
    //    [SSUIShareActionSheetStyle setCurrentPageIndicatorTintColor:[UIColor colorWithRed:156/255.0 green:156/255.0 blue:156/255.0 alpha:1.0]];
    //    [SSUIShareActionSheetStyle setPageIndicatorTintColor:[UIColor colorWithRed:62/255.0 green:62/255.0 blue:62/255.0 alpha:1.0]];
    
    //2、分享
    [ShareSDK showShareActionSheet:view
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                           
                       case SSDKResponseStateBegin:
                       {
                           
                           break;
                       }
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@", error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateCancel:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       default:
                           break;
                   }
                   
                   if (state != SSDKResponseStateBegin)
                   {
                       
                   }
                   
               }];
}
@end
